package com.coco.group.service.impl;

import com.coco.common.result.ResultData;
import com.coco.group.mapper.GroupBuyRecordMapper;
import com.coco.group.pojo.TbGroupBuyRecord;
import com.coco.group.service.GroupBuyRecordService;
import com.coco.group.vo.GroupBuyRecordVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Package: com.coco.group.service.impl
 * @Author: 张dy
 * @Date: Created in 2021/7/29 下午 8:04
 */
@Slf4j
@Service
public class GroupBuyRecordServiceImpl implements GroupBuyRecordService {
    
    @Autowired
    private GroupBuyRecordMapper groupBuyRecordMapper;

    @Override
    public List<TbGroupBuyRecord> findAll() {
        return groupBuyRecordMapper.findAll();
    }

    @Override
    public ResultData<TbGroupBuyRecord> search(GroupBuyRecordVo groupBuyRecordVo) {
        PageHelper.startPage(groupBuyRecordVo.getPage(),groupBuyRecordVo.getLimit());

        List<TbGroupBuyRecord> records= groupBuyRecordMapper.search(groupBuyRecordVo);
        PageInfo<TbGroupBuyRecord> pageInfo = new PageInfo<>(records);

        return new ResultData<>(pageInfo.getList(),pageInfo.getTotal());
    }

    @Override
    public void deleteById(Integer id) {
        groupBuyRecordMapper.deleteById(id);
    }

    /**
     * 添加拼团记录
     * @param record
     */
    @Override
    public void addRecord(TbGroupBuyRecord record) {
        groupBuyRecordMapper.addRecord(record);
        log.info("获取到的记录的id为:{}",record.getId());
    }


    /**
     * 根据订单查找拼团记录
     * @param tradeNo
     */
    @Override
    public TbGroupBuyRecord findByOrderId(String tradeNo) {

        return groupBuyRecordMapper.findByOrderId(tradeNo);
    }


    /**
     * 修改参与人数
     */
    @Override
    public void changeJoinNum(Long id) {
        groupBuyRecordMapper.changeJoinNum(id);
    }

    /**
     * 拼团完成
     */
    @Override
    public void changeStatusToSuccess(Long id) {
        groupBuyRecordMapper.changeStatusToSuccess(id);
    }

    /**
     * 根据邀请码查询单条
     * @param code
     * @return
     */
    @Override
    public TbGroupBuyRecord findByInvitationCode(String code) {
        return groupBuyRecordMapper.findByInvitationCode(code);
    }
}
